package com.qst.financing.controller;

import com.qst.financing.common.Result;
import com.qst.financing.common.StatusCode;
import com.qst.financing.context.BaseContext;
import com.github.pagehelper.PageInfo;
import com.qst.financing.common.Result;
import com.qst.financing.common.StatusCode;
import com.qst.financing.context.BaseContext;
import com.qst.financing.pojo.Finance;
import com.qst.financing.pojo.Intention;
import com.qst.financing.pojo.Recommender;
import com.qst.financing.service.BankService;
import com.qst.financing.service.FinanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "融资模块接口")
@RestController
@RequestMapping("/finance")
@CrossOrigin
public class FinanceController {
    @Autowired
    FinanceService financeService;
    @Autowired
    BankService bankService;

    @ApiOperation("查询融资意向")
    @GetMapping("/selectIntentionByName")
    public Result selectIntentionByName(){
        String name = BaseContext.getCurrentuserName();
        List<Intention> intentionList=financeService.selectIntentionByName(name);
        return new Result<>(true, StatusCode.OK,"查询成功",intentionList);
    }

    @ApiOperation("添加融资意向")
    @PostMapping("/insertIntentionByName")
    public Result insertIntentionByName(@RequestBody Intention intention){
        financeService.insertIntentionByName(intention);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    //修改融资意向
    @ApiOperation("修改融资意向")
    @PutMapping("/updateIntentionByName")
    public Result updateIntentionByName(@RequestBody Intention intention){
        //获取登陆的用户名
        String name = BaseContext.getCurrentuserName();
        intention.setUserName(name);
        financeService.updateIntentionByName(intention);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //删除融资意向
    @ApiOperation("删除融资意向")
    @DeleteMapping("/deleteIntentionByName")
    public Result deleteIntentionByName(){
        //获取登陆的用户名
        String name = BaseContext.getCurrentuserName();
        financeService.deleteIntentionByName(name);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //查询推荐融资人
    @ApiOperation("查询推荐融资人")
    @GetMapping("/selectRecommendByName")
    public Result selectRecommendByName(){
        //获取登陆的用户名
        String name = BaseContext.getCurrentuserName();
        List<Recommender> list = financeService.selectRecommend(name);
        return new Result(true, StatusCode.OK, "查询成功",list);
    }

    @Value("${bank.bankid}")
    private Integer bankid;

    //查询推荐融资人
    @ApiOperation(value = "分页查询推荐融资人")
    @GetMapping("/selectRecommend/{pageNum}")
    public Result selectRecommend(@PathVariable("pageNum") Integer pageNum) {
        PageInfo<Recommender> list = financeService.selectRecommendFY(pageNum);
        return new Result(true, StatusCode.OK, "查询成功",list);
    }

    //根据finance_id修改融资情报
    @ApiOperation(value = "根据id修改融资情报")
    @PutMapping("/update")
    public Result update(@RequestBody Finance finance) {
        financeService.updateByFinance(finance);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //根据finance_id查询融资情报
    @ApiOperation(value = "根据id查询融资情报")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
//
        Finance finance = financeService.selectByFinanceId(id);
        return new Result(true, StatusCode.OK, "查询成功", finance);
    }

    //根据银行id与申请人查询融资情报
    @ApiOperation(value = "根据银行id与申请人查询融资情报")
    @GetMapping("/selectByFinance/{keys}/{pageNum}")
    public Result<PageInfo> selectByFinance(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {

        Finance financeKey = new Finance();
        financeKey.setBankId(bankid);
        PageInfo<Finance> financePageInfo = financeService.selectByFinance(financeKey,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", financePageInfo);
    }

    //根据银行id查询融资情报
    @ApiOperation(value = "根据银行id查询融资情报")
    @GetMapping("/selectByBankId/{pageNum}")
    public Result<PageInfo> selectByBankId(@PathVariable("pageNum") Integer pageNum) {

        Finance financeKey = new Finance();
        financeKey.setBankId(bankid);
        PageInfo<Finance> financePageInfo = financeService.selectByFinance(financeKey,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", financePageInfo);
    }

    //根据银行id查询银行名称
    @GetMapping("/selectBankName")
    public Result selectBankName() {
        String bankName = bankService.selectBankName(bankid);
        return new Result(true, StatusCode.OK, "查询成功",bankName);
    }


}
